package org.bsc.mgwt.client.pulltorefresh;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.googlecode.mgwt.mvp.client.MGWTAbstractActivity;
import com.googlecode.mgwt.ui.client.widget.base.PullPanel.PullWidget;
import com.googlecode.mgwt.ui.client.widget.base.PullPanel.PullWidget.PullState;
import com.googlecode.mgwt.ui.client.widget.base.PullPanel.Pullhandler;;

/**
 * 
 * @author bsorrentino
 *
 * @param <T>
 */
public abstract class PullToRefreshActivity<VIEW extends PullToRefreshDisplay<?>> extends MGWTAbstractActivity {

	/** managed view */
	protected final VIEW view;

	/**
	 * 
	 * @param view
	 */
	public PullToRefreshActivity(VIEW view) {
		super();
		this.view = view;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {

		view.getPullPanel().setHeaderPullHandler( new Pullhandler() {

			@Override
		    public void onPullStateChanged(PullWidget pullWidget, PullState state) {
		    	
		    }
			
			@Override
		    public void onPullAction(PullWidget pullWidget) {
				loadData();
				
			}
			
			
		});
		
	}
	
	public abstract PullToRefreshActivity<?> loadData();
	public abstract boolean isLoading();
	public abstract void cancelLoadData();

	@Override
	public String mayStop() {
		
		if( isLoading() ) {
			return "Please hold on. This activity is stopping.";
		}
		
		return null; 
	}

	@Override
	public void onStop() {
		cancelLoadData();
	}
	
}
